const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchResult:false,
    screen:"订单类型",
    screenDown: {screenDown:["全部","成品","样品","其他"],screen:0},
    screenFlag: false,//控制综合下拉框是否显示
    screenColor: false,
    dataColor: false,
    dataTime:"日期选择", 
    dataTime1:"起始日期",
    dataTime2:"截止日期",
    dataFlag: false, //控制综合下拉框是否显示
    isShowAll:false,
    nowTime: getApp().globalData.nowTime,
    alreadyOrder:[],
    openId:'',
    pageIndex:1,
    pageSize:10,
    total: 0,
    tips: 0,
    info: '客户选择',
    infoValue: '',
    infoTurn: true,//客户选择默认开启
    userCode: '',
    userColor: false,
    userFlag: false,//控制综合下拉框是否显示
    nameCall: '精确搜索',
    nameColor: false,
    nameFlag: false,
    nameCallInfo: '',

    winHeight: 900
  },

  toDetail: function(e) {
    wx.navigateTo({
      url: '/template/business/userorder/order/order?DocEntry='+e.currentTarget.dataset.item,
    })
  },

  getInputValue:function(e) {
    this.setData({
      nameCallInfo: e.detail.value
    })
  },

  screenShow: function () {//综合下拉
    if (this.data.screenFlag){
      this.setData({
        screenFlag : !this.data.screenFlag,
        dataFlag: false,
        dataColor: false,
        userFlag: false,
        userColor: false,
        nameFlag: false,
        nameColor: false
      })
    }else{
      this.setData({
        screenFlag: !this.data.screenFlag,
        dataFlag: false,
        dataColor: false,
        userFlag: false,
        userColor: false,
        nameFlag: false,
        nameColor: false
      })
    }
  },

  dataShow: function () {//日期下拉
    if (this.data.dataFlag){
      this.setData({
        dataFlag : !this.data.dataFlag,
        screenFlag: false,
        screenColor: false,
        userFlag: false,
        userColor: false,
        nameFlag: false,
        nameColor: false
      })
    }else{
      this.setData({
        dataFlag: !this.data.dataFlag,
        screenFlag: false,
        screenColor: false,
        userFlag: false,
        userColor: false,
        nameFlag: false,
        nameColor: false
      })
    }
  },

  changeDate1(e) {
    console.log(e)
    this.setData({
      dataTime1: e.detail.value,
      // dataFlag: false,
      dataColor: true
    })
    // this.getProject()
  },
  changeDate2(e) {
    this.setData({
      dataTime2: e.detail.value,
      dataFlag: false,
      dataColor: true,
      pageIndex: 1,
      tips: 1
    })
    this.alreadyShow()
  },

  selectData: function(){
    this.setData({
      nameFlag: false,
      nameColor: true,
      pageIndex: 1,
      tips: 1
    })
    this.alreadyShow();
  },

  handletouchtart: function (event) {//点击透明背景隐藏下拉
    this.setData({
      screenFlag: false,
      dataFlag: false,
      userFlag: false,
      nameFlag: false
    })
  },

  screenChoice:function(e){//综合下拉选择子项
    console.log(e)
    this.setData({
      screen: e.currentTarget.dataset.item,
      "screenDown.screen": e.currentTarget.dataset.index,
      screenFlag: false,
      screenColor: true,
      pageIndex: 1,
      tips: 1
    })
    // this.alreadyShow()
  },

  alreadyShow: function(){
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: globalIp + 'home/ocrd/getORDRList',
      data: {
        sdate: that.data.dataTime1 == '起始日期' ? '' : that.data.dataTime1,
        edate: that.data.dataTime2 == '截止日期' ? '' : that.data.dataTime2,
        pageSize: this.data.pageSize,
        pageIndex: this.data.pageIndex,
        // value: parseInt(that.data.screenDown.screen) || 0,
        openId: wx.getStorageSync('userInfo').openid,
        cardCode: that.data.infoValue,
        value: that.data.nameCallInfo
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (that.data.tips == 1) { // 切换筛选条件
          that.setData({
            tips: 0,
            alreadyOrder: res.data.jsonObject.data
          })
        } else {
          that.setData({
            alreadyOrder: [...that.data.alreadyOrder, ...res.data.jsonObject.data]
          })
        }
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

    // 搜索框获取客户信息
    search: function (value) {
      var that = this;
      var cardInfo = [];
      var result = new Object();
      result.text = '';
      result.value = '';
      var resultList=[];
      wx.request({
        url: globalIp + 'home/ocrd/getCardInfo',
        data: {
          value: value,
          openId: that.data.openid || wx.getStorageSync('userInfo').openid,
          login: wx.getStorageSync('userInfo').login || 'N'
        },
        header: {
          'content-type': 'application/json'
        },
        method: 'POST',
        success: function(res) {
          cardInfo = res.data.cardInfo.data
          // console.log(res.data.cardInfo.data)
        },
        fail: function(res) {
          console.log("请求失败")
        }
      })
      return new Promise((resolve, reject) => {
        setTimeout(() => {
            // resolve([{text: '搜索结果', value: 1}, {text: '搜索结果2', value: 2}])
            if (cardInfo) {
              for (var i of cardInfo) {
                result.text = i.CardName;
                result.value = i.CardCode;
                resultList.push(result);
                result = {};
              }
            }
            resolve(resultList);
        }, 200)
      })      
    },

    selectResult: function (e) {
      console.log('select result', e.detail)
      this.setData({
        userFlag: false,
        userColor: true,
        userCode: e.detail.item.value,
        info: e.detail.item.text,
        infoValue: e.detail.item.value,
        pageIndex: 1,
        tips: 1
      })
      this.alreadyShow()
    },

    userShow: function () {//综合下拉
      if (this.data.userFlag){
        this.setData({
          userFlag : !this.data.userFlag,
          screenFlag: false,
          screenColor: false,
          dataFlag: false,
          dataColor: false,
          nameFlag: false,
          nameColor: false
        })
      }else{
        this.setData({
          userFlag: !this.data.userFlag,
          screenFlag: false,
          screenColor: false,
          dataFlag: false,
          dataColor: false,
          nameFlag: false,
          nameColor: false
        })
      }
    },

    nameShow: function () {//综合下拉
      if (this.data.nameFlag){
        this.setData({
          nameFlag : !this.data.nameFlag,
          screenFlag: false,
          screenColor: false,
          dataFlag: false,
          dataColor: false,
          userFlag: false,
          userColor: false
        })
      }else{
        this.setData({
          nameFlag: !this.data.nameFlag,
          screenFlag: false,
          screenColor: false,
          dataFlag: false,
          dataColor: false,
          userFlag: false,
          userColor: false
        })
      }
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      search: this.search.bind(this),
    })
    if (wx.getStorageSync('userInfo').CardCode != '') {
      this.setData({
        infoTurn: false
      })
    }
    this.alreadyShow()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      pageIndex: this.data.pageIndex + 1
    })
    console.log(this.data)
    this.alreadyShow()
  },

})